package top.lyc829.zzl_android.ui.team;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.MyTeamState;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.TeamSimpleBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class TeamViewModel extends ViewModel {

    private MutableLiveData<List<TeamSimpleBean>> teamSimplesLiveData;
    private MutableLiveData<MyTeamState> myTeamStateLiveData;

    public MutableLiveData<MyTeamState> getMyTeamStateLiveData() {
        if (myTeamStateLiveData == null) {
            myTeamStateLiveData = new MutableLiveData<>();
        }
        return myTeamStateLiveData;
    }


    public MutableLiveData<List<TeamSimpleBean>> getTeamSimplesLiveData() {
        if (teamSimplesLiveData == null) {
            teamSimplesLiveData = new MutableLiveData<>();
        }
        return teamSimplesLiveData;
    }


    public void getTeams(int page, String location, String searchContent) {
        Network.getTeams(page, location, searchContent, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<List<TeamSimpleBean>> resultBean = JsonUtilNew.fromJsonArray(resStr, TeamSimpleBean.class);

                if (resultBean.getCode().equals(200)) {
                    getTeamSimplesLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
                getTeamSimplesLiveData().postValue(null);
            }
        });
    }

    public void getMyTeamIdFromServer(int userId) {
        Network.getIsInTeam(userId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<MyTeamState> resultBean = JsonUtilNew.fromJsonObject(resStr, MyTeamState.class);
                // 无团队
//                if (resultBean.getCode().equals(361)) {
//                    getMyTeamStateLiveData().postValue(new MyTeamState("-1", -1));
//                }
                if (resultBean.getCode().equals(200)) {
                    if (resultBean.getData() == null) {
                        getMyTeamStateLiveData().postValue(null);
                    }
                    getMyTeamStateLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }



}